package com.vitabuybuy.tv.ui.mine.comment

import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import cc.shinichi.library.ImagePreview
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityCommentDetailBinding
import com.vitabuybuy.tv.dto.OrderDetailDto
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.mine.comment.viewmodel.MyCommentViewModel
import com.vitabuybuy.tv.ui.shopcenter.adapter.CommentImageListAdapter
import com.vitabuybuy.tv.ui.tencent.superplay.SuperPlayVidelMainActivity
import com.vitabuybuy.tv.utils.EventCode
import kotlinx.android.synthetic.main.activity_comment_detail.*
import org.greenrobot.eventbus.EventBus

class CommentDetailActivity : BaseActivity<MyCommentViewModel, ActivityCommentDetailBinding>() {

    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_comment_detail

    private val commentImageListAdapter by lazy { CommentImageListAdapter() }

    private var commentId :Int?= -1
    private var position :Int?= -1

    override fun onGetBundle(bundle: Bundle?) {
        commentId = bundle?.getInt("commentId")
        position = bundle?.getInt("position")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.comment_center))
        mBinding.viewModel = viewModel
        listView.apply {
            adapter = commentImageListAdapter
            layoutManager = GridLayoutManager(context, 4)
        }
    }

    override fun observeData() {
        viewModel.getCommentDetail(commentId)
        viewModel.commentDetail.observe(this, Observer {
            iv_score.rating = it.scores.toFloat()
            val images = ArrayList<OrderDetailDto.ItemListBean.VideoAndPicBean>()
            if (!it.images.isNullOrEmpty()) {
                for (image in it.images.split(",")) {
                    val bean = OrderDetailDto.ItemListBean.VideoAndPicBean()
                    bean.fileUrl = image
                    bean.tag = "image"
                    images.add(bean)
                }
            }
            if (!it.videos.isNullOrEmpty()) {
                val bean = OrderDetailDto.ItemListBean.VideoAndPicBean()
                bean.fileUrl = it.videos
                bean.cover = it.cover
                bean.tag = "video"
                images.add(bean)
            }
            commentImageListAdapter.setNewData(images)
        })
    }

    override fun eventHandling() {
        commentImageListAdapter.setOnItemClickListener { _, item, position ->
            if (item.tag == "video") {
                SuperPlayVidelMainActivity.openActivity(mContext, item.fileUrl,item.cover)
            } else {
                val images = ArrayList<String>()
                for (bean in commentImageListAdapter.mData) {
                    if (bean.tag == "image") {
                        images.add(bean.fileUrl)
                    }
                }
                ImagePreview.getInstance().setContext(mContext)
                    .setIndex(0)
                    .setShowDownButton(false)
                    .setImageList(images)
                    .setLoadStrategy(ImagePreview.LoadStrategy.NetworkAuto)
                    .setZoomTransitionDuration(300)
                    .setEnableDragClose(true)
                    .setErrorPlaceHolder(R.drawable.default_image).start()
            }
        }
        btnDelete.setOnClickListener {
            AlertDialog.Builder(mContext)
                .setTitle(getString(R.string.logout))
                .setMessage(getString(R.string.are_you_sure_delete_this_comment))
                .setCancelable(true)
                .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                    viewModel.delete(commentId, position)
                }
                .setNegativeButton(getString(R.string.cancel)) { _, _ -> }.create()
                .show()
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            2 -> {
                XToast.success(getString(R.string.already_delete))
                EventBus.getDefault().post(MessageEvent(EventCode.delete_comment, position))
                finish()
            }
        }
    }


    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, commentId: Int, position: Int) {
            mContext.startActivity(
                bundleOf(
                    Pair("commentId", commentId),
                    Pair("position", position)
                ),
                CommentDetailActivity::class.java
            )
        }
    }
}
